const MagicString = require("magic-string"); //rollup作者为了写rollup单独写的一个库
const analyse = require("./ast/analyse");
const {parse} = require('acorn');

//每个文件都是一个模块
//每个模块都会对应一个Module实例
class Module {
  constructor({code,path,bundle}) {
    // Advanced: a source can include an `indentExclusionRanges` property
    // alongside `filename` and `content`. This will be passed to `s.indent()`
    this.code = new MagicString(code, {filename: path} /*←目前没撒用*/);

    this.path = path;
    this.bundle = bundle;

    this.ast = parse(code, {
      ecmaVersion: 7,
      sourceType: 'module'
    });

    this.analyse();
  }

  analyse() {
    //目前就是在 ast 的每一条语句挂了一个 ._source属性
    //即该条ast statement 对应的 源码
    analyse(this.ast, this.code, this);
  }

  //展开所有节点
  expandAllStatements(){
    const allStatements = [];
    this.ast.body.forEach(statement => {
      const statements = this.expandStatement(statement);
      allStatements.push(...statements);
    });
    return allStatements;
  }

  //展开一个节点
  expandStatement(statement) {
    const result = [];

    if(!statement._included){
      statement._included = true;
      result.push(statement);
    }

    return result;
  }
}

module.exports = Module;
